package com.polymorphic;

/***
 * 测试java多态  和参数类型匹配 <p>
 * @author LingMin 
 * @date 2017年9月29日<br>
 * @version 1.0<br>
 */
public class MixTest {

	static class Human {
		 
        public String sayHello(MixTest.Human human) {
            return "你好";
        }
 
        public String sayHello(MixTest.Man human) {
            return "您好,先生";
        }
 
        public String sayHello(MixTest.Woman human) {
            return "您好,美女";
        }
 
       /* public String sayHello(MixTest.Girl human) {
            return "您好,美少女";
        }*/
    }
 
    static class Man extends MixTest.Human {
 
        public String sayHello(MixTest.Human human) {
            return "你好,我是Y先生";
        }
 
        public String sayHello(MixTest.Woman human) {
            return "您好,美女,我是Y先生";
        }
 
        public String sayHello(MixTest.Girl human) {
            return "您好,美少女,我是Y先生";
        }
 
        // 先生对先生比较谨慎,没那么快介绍自己 =。=
    }
 
    static class Woman extends MixTest.Human {
 
        public String sayHello(MixTest.Human human) {
            return "你好,我是X美女";
        }
 
        public String sayHello(MixTest.Woman human) {
            return "您好,美女,我是X美女";
        }
 
        public String sayHello(MixTest.Girl human) {
            return "您好,美少女,我是X美女";
        }
 
        // 美女对先生比较含蓄,没那么快介绍自己 =。=
    }
 
    static class Girl extends MixTest.Woman {
 
        public String sayHello(MixTest.Human human) {
            return "你好,我是O美少女";
        }
 
    }
 
    public static void main(String[] args) {
        MixTest test = new MixTest();
        MixTest.Human guy = new MixTest.Human();
        MixTest.Human manAsGuy = new MixTest.Man();
        MixTest.Man man = new MixTest.Man();
        MixTest.Human womanAsGuy = new MixTest.Woman();
        MixTest.Woman woman = new MixTest.Woman();
        MixTest.Girl girl = new MixTest.Girl();
 
        System.out.print("假设大家在QQ等聊天软件上认识,这时候一般来招呼如下");
        System.out.println("当然先生对先生比较谨慎,没那么快介绍自己：");
        printMessage("1", guy.sayHello(guy),  "[]");
        printMessage("2", guy.sayHello(man),"[]");
        printMessage("3", guy.sayHello(woman), "[]");
        printMessage("4", manAsGuy.sayHello(guy), "");
        printMessage("5", manAsGuy.sayHello(manAsGuy), "");
        printMessage("6", manAsGuy.sayHello(womanAsGuy), "");
        printMessage("7", manAsGuy.sayHello(man), "");
        printMessage("8", manAsGuy.sayHello(woman), "");
        
        /**********************************这个方法 调用不好理解
         * 
         * 结果9：

			MixTest.Human manAsGuy = new MixTest.Man();
			
			manAsGuy.sayHello(girl),  
			
			因为manAsGuy 声明是Human 类，方法从Human类开始搜索，Human类没有欢迎Girl的方法，
			
			因此按照最适合方法版本，兼容找到了Human 类的欢迎Woman的方法，
			
			又因为实际类型是Man类，该方法有重写，因此实际执行了Man类的欢迎Woman的方法。
         * *********************************/
        printMessage("9", manAsGuy.sayHello(girl),  "");
        
        
        printMessage("10 ", man.sayHello(guy),"");
        printMessage("11", man.sayHello(manAsGuy),  "");
        printMessage("12", man.sayHello(womanAsGuy), "");
        printMessage("13", man.sayHello(man), "");
        printMessage("14", man.sayHello(woman),  "");
        printMessage("15", man.sayHello(girl), "");
    }
 
    private static volatile int index = 1;
 
    private static void printMessage(String title, String message, String narrator) {
        System.out.println((index++) + "、" + String.format("%-5s%-5s%s", new String[]{title, message, narrator}));
    }
 
}
